package com.hcb.tonguetiphealth.widget.resbook;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hcb.tonguetiphealth.adapter.GeneralPageAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.ResDish;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.LoginActivity;
import com.hcb.tonguetiphealth.view.MoreActivity;
import com.hcb.tonguetiphealth.view.OnlineBookActivity;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.view.SearchActivity;
import com.hcb.tonguetiphealth.widget.CommentWidget;
import com.hcb.tonguetiphealth.widget.GeneralWidget;
import com.hcb.tonguetiphealth.widget.more.HistoryWidget;

public class RestaurantBookInfoWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "ResBookInfoWidget";
	
	private Activity ctx;
	private Handler mHandler , handler;
	private LayoutInflater inflater;
	private List<Restaurant> resList;
	private TextView resName, resLevel, resrjxf, resKw, resHj, resFw, resContent, resAddrss, resTel;
	private ImageView iv, iv1, iv2, iv3, iv4, iv5, iv6;
	private ImageView dishIv1,dishIv2,dishIv3,dishIv4, dishIv5, dishIv6;
	private String userid;//商户id
	private ImageView resImg;
	private RatingBar resRb;
	private Restaurant res = new Restaurant();
	private LinearLayout llResContent;
	private ImageView ivUporDown;
	private Button btCallRes,btSendMsg, btBookDish;
	private LinearLayout llResInfo, llNearRes , llback;
	private boolean sendSucess = false;
	
	private String fromWidget;
	
	//底部按钮
	private LinearLayout llCity, llSearch, llHotRes, llLogin, llMore ;
	private ImageView imgCity, imgSearch, imgHotRes, imgLogin, imgMore;
	
	private String strSearch;
	private Spanned sp;
	
	//附近商家
	private ViewPager viewPager;
	private List<View> pageViews;
	private View view;
	private GeneralPageAdapter pageAdapter;
	private int crrentPageId = 0;
	
	//商家菜品
	private ViewPager viewPager2;
	private List<View> pageViews2;
	private View view2;
	private GeneralPageAdapter pageAdapter2;
	private int crrentPageId2 = 0;
	
	private List<ResDish>resDishList ;
	
	public RestaurantBookInfoWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.res_book_res_info, this); 
		inflater = ctx.getLayoutInflater();
		userid = ctx.getIntent().getStringExtra("resId");
		strSearch = ctx.getIntent().getStringExtra("Search");
		fromWidget = ctx.getIntent().getStringExtra("fromWidget");
		bindView();
		bindEvent();
		initData();
	//	resList = Global.resList;
//		resList = new ArrayList<Restaurant>();
//		for (int i = 0; i < Global.resList.size() && i < 10 ; i++) {
//			resList.add(Global.resList.get(i));
//		}//加载10个附近商家
		getResDishs();
	//	initDataPageView();
	}
	
	private void bindEvent() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取商家信息...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					initResInfo();
					dismissMessageDialog();
					break;
				case MessageWhat.NETWORK_FAILD:
					try {
					  dismissMessageDialog();
					} catch (Exception e) {
						// TODO: handle exception
					}
					//Toast.makeText(ctx,  "网络有点不给力哦", Toast.LENGTH_LONG).show();
					showMessage(ctx, R.string.net_faild);
					break;
				case MessageWhat.GETDISHS_SUCESS:
					initDataPageView2();
					break;
				case MessageWhat.SHOW_DIALOG_SENDMSG:
					showMessageDialog(ctx, "正在发送信息...");
					break;
				case MessageWhat.DISMISSDIALOG_SEND:
					dismissMessageDialog();
					String tipMsg;
					if (sendSucess) {
						tipMsg = "发送成功,请稍后查收短信！";
					}else {
						tipMsg = "发送失败！";
					}
					Toast.makeText(ctx, tipMsg, Toast.LENGTH_LONG).show();
					break;
				}
			}
		};
		llResContent.setOnClickListener(this);
		btCallRes.setOnClickListener(this);
		llback.setOnClickListener(this);
		btSendMsg.setOnClickListener(this);
		llNearRes.setOnClickListener(this);
		llResInfo.setOnClickListener(this);
		btBookDish.setOnClickListener(this);
		//底部按钮
		llCity.setOnClickListener(this);
		llSearch.setOnClickListener(this);
		llHotRes.setOnClickListener(this);
		llLogin.setOnClickListener(this);
		llMore.setOnClickListener(this);
		//底部按钮
		bindTouchEvent(llCity, imgCity);
		bindTouchEvent(llSearch , imgSearch);
		bindTouchEvent(llHotRes, imgHotRes);
		bindTouchEvent(llLogin, imgLogin);
		bindTouchEvent(llMore,imgMore);
	}
	
	private void bindView() {
		resName = (TextView)findViewById(R.id.res_info_name_textview);
		resImg = (ImageView)findViewById(R.id.res_book_res_info_img);
		resRb = (RatingBar)findViewById(R.id.res_info_level_ratings_ratingbar);
		resrjxf = (TextView)findViewById(R.id.res_book_res_info_rjxf_textview);
		resKw = (TextView)findViewById(R.id.res_book_res_info_kw_textview);
		resHj = (TextView)findViewById(R.id.res_book_res_info_hj_textview);
		resFw = (TextView)findViewById(R.id.res_book_res_info_fw_textview);
		resContent = (TextView)findViewById(R.id.res_book_res_info_info_tv);
		resAddrss = (TextView)findViewById(R.id.res_book_res_info_address_textview);
		resTel = (TextView)findViewById(R.id.res_book_res_info_tel_textview);
		llResContent = (LinearLayout)findViewById(R.id.res_book_res_content_linearlayout);
		//viewPager = (ViewPager)findViewById(R.id.res_info_resnear_viewpager);
		ivUporDown = (ImageView)findViewById(R.id.res_book_res_content_upordown);
		viewPager2 = (ViewPager)findViewById(R.id.res_info_dish_viewpager);
		btCallRes = (Button)findViewById(R.id.res_book_res_info_call_bt);
		llback = (LinearLayout)findViewById(R.id.res_info_back);
		btSendMsg = (Button)findViewById(R.id.res_info_address_bt);
		//btToNearRes = (Button)findViewById(R.id.res_book_nearres_bt);
		llResInfo = (LinearLayout)findViewById(R.id.res_book_info_comment_ll);
		btBookDish = (Button)findViewById(R.id.res_info_book_bt);
		
		llNearRes = (LinearLayout)findViewById(R.id.res_book_nearres_linearlayout);
		
		//底部按钮
		llCity = (LinearLayout)findViewById(R.id.linearlayout_home_city);
		llSearch = (LinearLayout)findViewById(R.id.linearlayout_home_search);
		llHotRes = (LinearLayout)findViewById(R.id.linearlayout_home_res);
		llLogin = (LinearLayout)findViewById(R.id.linearlayout_home_login);
		llMore = (LinearLayout)findViewById(R.id.linearlayout_home_more);
		//底部按钮
		imgCity = (ImageView)findViewById(R.id.img_home_city);
		imgSearch = (ImageView)findViewById(R.id.img_home_search);
		imgHotRes = (ImageView)findViewById(R.id.img_home_hotres);
		imgLogin = (ImageView)findViewById(R.id.img_home_login);
		imgMore = (ImageView)findViewById(R.id.img_home_more);
	}
 
	
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.res_book_res_content_linearlayout:
			int visibililty = resContent.getVisibility();
			if (visibililty == View.GONE || visibililty == View.INVISIBLE) {
				resContent.setVisibility(View.VISIBLE);
				ivUporDown.setBackgroundResource(R.drawable.close_more);
			}else {
				ivUporDown.setBackgroundResource(R.drawable.open_more);
				resContent.setVisibility(View.GONE);
			}
			break;
		case R.id.res_book_res_info_call_bt:
			if (res.getTelephone() == null && res.getTelephone().equals("")) {
				Toast.makeText(ctx, "电话号码为空！", Toast.LENGTH_LONG).show();
			}else {
				intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + res.getTelephone()));
				ctx.startActivity(intent);
			}
			break;
		case R.id.res_info_back:
			if ("true".equals(strSearch)) {
				ctx.finish();
			}else if ( "HistoryWidget".equals(fromWidget) ) {
				ctx.setContentView(new HistoryWidget(ctx));
			}else {
				RestaurantBookWidget widget = new RestaurantBookWidget(ctx);
				ctx.setContentView(widget);
			}
			break;
		case R.id.res_info_address_bt:
			if (res.getAddress() == null || res.getAddress().equals("")) {
				Toast.makeText(ctx, "商家地址为空！", Toast.LENGTH_LONG).show();
			}else {
				if (Global.user == null) {
					Toast.makeText(ctx, "请登录！", Toast.LENGTH_LONG).show();
					login();
				}else {
					SendMsg();
				}
			}
			break;
		case R.id.res_book_nearres_linearlayout:
			RestaurantBookWidget widget = new RestaurantBookWidget(ctx);
			ctx.setContentView(widget);
			break;
		case R.id.res_book_info_comment_ll:
			//进入商家评论
			ctx.getIntent().putExtra("typeId", "4");
			ctx.getIntent().putExtra("targetId", userid);
			ctx.getIntent().putExtra("commentSucceed", false);
			ctx.setContentView(new CommentWidget(ctx));
			break;
		case R.id.res_info_book_bt:
			intent.putExtra("resId", userid);
			intent.setClass(ctx, OnlineBookActivity.class);
			ctx.startActivity(intent);
			//底部按钮
		case R.id.linearlayout_home_city:
			break;
		case R.id.linearlayout_home_search:
			intent.setClass(ctx, SearchActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_res:
			break;
		case R.id.linearlayout_home_login:
			intent.setClass(ctx, LoginActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
		case R.id.linearlayout_home_more:
			intent.setClass(ctx, MoreActivity.class);
			ctx.startActivity(intent);
			ctx.finish();
			break;
			//底部按钮
		default:
		//	initResInfo(v.getId());
			break;
		}
	}
	
	private void initResInfo(){
		resName.setText(res.getName());
		loadImage(resImg, res.getImgURL());
		resRb.setRating(res.getLevel());
		resrjxf.setText("null".equals(res.getAvg())?"":res.getAvg() + " 元");
		//resContent.setText(res.getContent());
	//	resContent.setText(StringUtils.replaceCharacter(res.getContent()));
		//http://blog.csdn.net/zhang957411207/article/details/7741972
		//android textview处理html
		resContent.setText(sp);
//		resContent.setText();
		resAddrss.setText(res.getAddress());
		resTel.setText(res.getTelephone());
		resKw.setText(res.getResKw());
		resHj.setText(res.getResHj());
		resFw.setText(res.getResFw());
		
	}
	
	/**
	 * 按钮触碰事件;touch后修改背景以及图标
	 */
	public void bindTouchEvent(final LinearLayout linearLayout, final ImageView img) {
		linearLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					linearLayout.setBackgroundResource(R.drawable.bottom_menu_bg);
					switch (img.getId()) {
 					case R.id.img_home_city:
 						img.setBackgroundResource(R.drawable.location_yellow);
						break;
					case R.id.img_home_search:
						img.setBackgroundResource(R.drawable.search_yellow);
						break;
					case R.id.img_home_hotres:
						img.setBackgroundResource(R.drawable.hotres_yellow);
						break;
					case R.id.img_home_login:
						img.setBackgroundResource(R.drawable.login_yellow);
						break;
					case R.id.img_home_more:
						img.setBackgroundResource(R.drawable.more_yellow);
						break;
					default:
						break;
					}
				} else if (event.getAction() == KeyEvent.ACTION_UP) {
					linearLayout.setBackgroundResource(R.drawable.main_gray_bg);
					switch (img.getId()) {
 					case R.id.img_home_city:
 						img.setBackgroundResource(R.drawable.location_red);
						break;
					case R.id.img_home_search:
						img.setBackgroundResource(R.drawable.search_red);
						break;
					case R.id.img_home_hotres:
						img.setBackgroundResource(R.drawable.hotres_red);
						break;
					case R.id.img_home_login:
						img.setBackgroundResource(R.drawable.login_red);
						break;
					case R.id.img_home_more:
						img.setBackgroundResource(R.drawable.more_red);
						break;
					default:
						break;
					}
				}
				return false;
			}
		});
	}
	
	private void initResInfo(int id) {
		int num = 0;
		switch(id) {
		case R.id.drinks_book_drink_info_pageview_img1:
			num += 0;
			break;
		case R.id.drinks_book_drink_info_pageview_img2:
			num += 1;
			break;
		case R.id.drinks_book_drink_info_pageview_img3:
			num += 2;
			break;
		case R.id.drinks_book_drink_info_pageview_img4:
			num += 3;
			break;
		case R.id.drinks_book_drink_info_pageview_img5:
			num += 4;
			break;
		case R.id.drinks_book_drink_info_pageview_img6:
			num += 5;
			break;
		}
		int position = crrentPageId*6 + num;
		Restaurant res = resList.get(position);
		if(res.isCurrent()) {
			showMessage(ctx, "已是当前商家详情");
		} else {
			for (Restaurant restaurant : resList) {
				if (restaurant!=res) {
					restaurant.setCurrent(false);
				}else {
					res.setCurrent(true);
				}
			}
			userid = res.getId();
			initData();
		}
	}
	
	private void SendMsg(){
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG_SENDMSG);
		new Thread(){
			public void run() {
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("userid", Global.user.getUserId()));
				params.add(new BasicNameValuePair("resName", res.getName()));
				params.add(new BasicNameValuePair("resAddress", res.getAddress()));
				String respone = HttpUtils.Post(Global.getServletressendmsg(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+respone);
				}
				if (respone != null) {
					try {
						JSONObject jsonObject = new JSONObject(respone);
						 String msg = jsonObject.getString("msg");
						 String strReturn = jsonObject.getString("return");
						 if ("true".equals(strReturn)) {
							sendSucess = true;
						}else {
							sendSucess = false;
						}
					} catch (JSONException e) {
						Log.e(TAG, "sendMsg", e);
					} catch (Exception e) {
						Log.e(TAG, "sendMsg", e);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DISMISSDIALOG_SEND);
			}
		}.start();
	}
	
	private void initData() {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread(){
			public void run() {
				res = new Restaurant();
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("userid",userid));
				String result  = HttpUtils.Post(Global.getServletResInfo(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+result);
				}
				if (result != null) {
					JSONManager jsonManager = new JSONManager();
					boolean success = jsonManager.isGotJSONObject(result);
					if (success) {
						JSONArray jsonArray = jsonManager.getJsonList();
						for(int i=0; jsonArray != null  && i < jsonArray.length(); i++) {
							try {
								res.setCurrent(true);
								JSONObject obj = jsonArray.getJSONObject(i);
								if(i==0){
									res.setName(obj.getString("company"));
									res.setImgURL(obj.getString("thumb"));
									res.setAvg(obj.getString("rjxf"));
									res.setAddress(obj.getString("address"));
									res.setTelephone(obj.getString("telephone"));
								}else if (i == 1) {
									res.setContent(obj.getString("content"));
								}else if (i == 2) {
									res.setHtml_content(obj.getString("html"));
								}else if (i == 3) {
									res.setLevel(StringUtils.parseFloat(obj.getString("level")));
								}else if (i == 4) {
									res.setResKw(obj.getString("produce"));
								}else if (i == 5) {
									res.setResHj(obj.getString("environment"));
								}else if (i == 6) {
									res.setResFw(obj.getString("serve"));
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					String html = res.getHtml_content();
					sp = Html.fromHtml(html, new Html.ImageGetter() {
						@Override
						public Drawable getDrawable(String source) {
							InputStream is = null;
							try {
								is = (InputStream) new URL(source).getContent();
								Drawable d = Drawable.createFromStream(is, "src");
//								d.setBounds(0, 0, d.getIntrinsicWidth(),
//										d.getIntrinsicHeight());
								d.setBounds(0, 0, 250,//图片宽度、高度设置
										150);
								is.close();
								return d;
							} catch (Exception e) {
								return null;
							}
						}
					}, null);
					mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
				}else {
					mHandler.sendEmptyMessage(MessageWhat.NETWORK_FAILD);
				}
				
			}
		}.start();
	}
	
	private void getResDishs(){
		resDishList = new ArrayList<ResDish>();
		new Thread(){
			public void run() {
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("userid",userid));
				String result  = HttpUtils.Post(Global.getServletresdishs(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+result);
				}
				JSONManager jsonManager = new JSONManager();
				boolean success = jsonManager.isGotJSONObject(result);
				if (success) {
					JSONArray jsonArray = jsonManager.getJsonList();
					for(int i=0; jsonArray != null  && i < jsonArray.length() && i < 10; i++) {//获取前10个
						try {
							ResDish resDish = new ResDish();
							JSONObject obj = jsonArray.getJSONObject(i);
							resDish.setId(obj.getString("itemid"));
							resDish.setTitle(obj.getString("title"));
							resDish.setDishThumb(obj.getString("dish_thumb"));
							resDish.setNum(obj.getString("num"));
							resDish.setPrice(obj.getString("price"));
							resDishList.add(resDish);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				
				mHandler.sendEmptyMessage(MessageWhat.GETDISHS_SUCESS);
			}
		}.start();
		
	}
	@Override
	public void notifyAdapter() {
		// TODO Auto-generated method stub
		
	}
	
	private void initDataPageView() {
//		for(Restaurant res : resList) {
//			if(drinks.isCurrent()) {
//				crrentDrinks = drinks;
////				downLoadData();
//				break;
//			}
//		}
		pageViews = new ArrayList<View>();
		int pageCount = resList.size()/6 + (resList.size()%6 == 0?0:1);
		for(int i=0; i< pageCount; i++) {
			pageViews.add(inflater.inflate(R.layout.drinks_book_drinks_info_pageview, null));
		}
		
		Restaurant res;
		
		for(int i=0; i<pageViews.size(); i++) {
			view = pageViews.get(i);
			iv1 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img1);
			iv2 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img2);
			iv3 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img3);
			iv4 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img4);
			iv5 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img5);
			iv6 = (ImageView)view.findViewById(R.id.drinks_book_drink_info_pageview_img6);
			int count = resList.size() - (i+1)*6;
			if(count >= 0) {
				for(int j=1; j<=6; j++) {
					int igg = i*6 + j-1;
					res = resList.get(i*6 + j-1);
					initImage(j, res);
				}
			} else {
				count = resList.size() - i*6;
				for(int j=1; j<=count; j++) {
					res = resList.get(i*6 + j-1);
					initImage(j, res);
				}
				switch(count) {
				case 1:
					iv2.setVisibility(ImageView.INVISIBLE);
					iv3.setVisibility(ImageView.INVISIBLE);
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 2:
					iv3.setVisibility(ImageView.INVISIBLE);
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 3:
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 4:
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 5:
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				}
			}
			
		}
		 
			pageAdapter = new GeneralPageAdapter(pageViews);
			viewPager.setAdapter(pageAdapter);
			viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}
	
	private void initDataPageView2() {//商家菜品
//		for(Restaurant res : resList) {
//			if(drinks.isCurrent()) {
//				crrentDrinks = drinks;
////				downLoadData();
//				break;
//			}
//		}
		pageViews2 = new ArrayList<View>();
		int pageCount = resDishList.size()/6 + (resDishList.size()%6 == 0?0:1);
		for(int i=0; i< pageCount; i++) {
			pageViews2.add(inflater.inflate(R.layout.drinks_book_drinks_info_pageview, null));
		}
		
		ResDish resDish;
		
		for(int i=0; i<pageViews2.size(); i++) {
			view2 = pageViews2.get(i);
			dishIv1 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img1);
			dishIv2 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img2);
			dishIv3 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img3);
			dishIv4 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img4);
			dishIv5 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img5);
			dishIv6 = (ImageView)view2.findViewById(R.id.drinks_book_drink_info_pageview_img6);
			int count = resDishList.size() - (i+1)*6;
			if(count >= 0) {
				for(int j=1; j<=6; j++) {
					resDish = resDishList.get(i*6 + j-1);
					initImage2(j, resDish);
				}
			} else {
				count = resDishList.size() - i*6;
				for(int j=1; j<=count; j++) {
					resDish = resDishList.get(i*6 + j-1);
					initImage2(j, resDish);
				}
				switch(count) {
				case 1:
					dishIv2.setVisibility(ImageView.INVISIBLE);
					dishIv3.setVisibility(ImageView.INVISIBLE);
					dishIv4.setVisibility(ImageView.INVISIBLE);
					dishIv5.setVisibility(ImageView.INVISIBLE);
					dishIv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 2:
					dishIv3.setVisibility(ImageView.INVISIBLE);
					dishIv4.setVisibility(ImageView.INVISIBLE);
					dishIv5.setVisibility(ImageView.INVISIBLE);
					dishIv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 3:
					dishIv4.setVisibility(ImageView.INVISIBLE);
					dishIv5.setVisibility(ImageView.INVISIBLE);
					dishIv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 4:
					dishIv5.setVisibility(ImageView.INVISIBLE);
					dishIv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 5:
					dishIv6.setVisibility(ImageView.INVISIBLE);
					break;
				}
			}
			
		}
		 
			pageAdapter2 = new GeneralPageAdapter(pageViews2);
			viewPager2.setAdapter(pageAdapter2);
			viewPager2.setOnPageChangeListener(new GuidePageChangeListener());
	}
	
	
	private void initImage(int id, Restaurant res) {
		switch(id) {
		case 1:
			loadImage(iv1, res.getImgURL());
			iv1.setOnClickListener(this);
			break;
		case 2:
			loadImage(iv2, res.getImgURL());
			iv2.setOnClickListener(this);
			break;
		case 3:
			loadImage(iv3, res.getImgURL());
			iv3.setOnClickListener(this);
			break;
		case 4:
			loadImage(iv4, res.getImgURL());
			iv4.setOnClickListener(this);
			break;
		case 5:
			loadImage(iv5, res.getImgURL());
			iv5.setOnClickListener(this);
			break;
		case 6:
			loadImage(iv6, res.getImgURL());
			iv6.setOnClickListener(this);
			break;
		}
	}
	
	private void initImage2(int id, ResDish resDish) {
		switch(id) {
		case 1:
			loadImage(dishIv1, resDish.getDishThumb());
		//	dishIv1.setOnClickListener(this);
			break;
		case 2:
			loadImage(dishIv2, resDish.getDishThumb());
		//	dishIv2.setOnClickListener(this);
			break;
		case 3:
			loadImage(dishIv3, resDish.getDishThumb());
		//	dishIv3.setOnClickListener(this);
			break;
		case 4:
			loadImage(dishIv4, resDish.getDishThumb());
		//	dishIv4.setOnClickListener(this);
			break;
		case 5:
			loadImage(dishIv5, resDish.getDishThumb());
		//	dishIv5.setOnClickListener(this);
			break;
		case 6:
			loadImage(dishIv6, resDish.getDishThumb());
		//	dishIv6.setOnClickListener(this);
			break;
		}
	}
	// 指引页面更改事件监听器
	class GuidePageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int arg0) {
			crrentPageId = arg0;
			switch(arg0) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			}
		}
	}
}
